package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.QValueConfig;
import com.xinhe.web.entity.ValueConfig;
import com.xinhe.web.redis.ValueConfigCacheRedis;
import com.xinhe.web.reposity.ValueConfigRepository;
import com.xinhe.web.vo.ValueConfigModel;
import com.xinhe.web.vo.ValueConfigView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;

@Service
public class ValueConfigService {
    private static final String CONFIG_TYPE_SYSTEM = "SYSTEM";
    @Autowired
    private ValueConfigRepository valueConfigRepository;
    @Autowired
    private ValueConfigCacheRedis valueConfigCacheRedis;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public Result<ValueConfig> detailValueConfig(String valueConfigId) {
        ValueConfig valueConfig = this.valueConfigRepository.findById(valueConfigId).get();
        return Result.ok(valueConfig);
    }
    public Result deleteValueConfig(String valueConfigId) {
        ValueConfig temp = this.valueConfigRepository.findById(valueConfigId).get();
        if ("SYSTEM".equals(temp.getType())) {
            return Result.failed(30001, "系统配置不能删除");
        } else {
            this.valueConfigRepository.delete(temp);
            this.valueConfigCacheRedis.delete(temp.getId());
        }
        return Result.ok();
    }

    @Transactional
    public Result<ValueConfig> saveValueConfig(ValueConfigModel model) {
        ValueConfig temp = null;
        if (StringUtils.isNotBlank(model.getId())) {
            temp = this.valueConfigRepository.findById(model.getId()).get();
        }
        if (null == temp) {
            temp = new ValueConfig();
            temp.setId(model.getId());
        }
        temp.setRemark(model.getRemark());
        temp.setType(model.getType());
        temp.setValue(model.getValue());
        this.valueConfigRepository.save(temp);
        this.valueConfigCacheRedis.put(new ValueConfigView(temp.getId(), temp.getValue(), temp.getType(), temp.getRemark()));
        return Result.ok(temp);
    }

    public QueryResults<ValueConfigView> pageValueConfig(String type, int pageNum, int pageSize) {
        QValueConfig qValueConfig = QValueConfig.valueConfig;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(type)){
            builder.and(qValueConfig.type.eq(type));
        }
        return queryFactory.select(
                Projections.bean(
                        ValueConfigView.class,
                        qValueConfig.id,
                        qValueConfig.value,
                        qValueConfig.type,
                        qValueConfig.remark
                )
        ).from(qValueConfig)
                .where(builder).offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }
}
